from fastapi import APIRouter, Request, Query
from typing import Optional
from pydantic import BaseModel
from finance.python.middleware.auth import token_required
from finance.python.middleware.logging import log_required
from finance.python.services import family_service

# 创建路由器
family_router = APIRouter(
    prefix="/api",
    tags=["family"]
)

class CreateFamilyRequest(BaseModel):
    family_name: str

@family_router.post("/create_family", summary="创建家庭")
@token_required
@log_required
async def create_family(request: Request, data: CreateFamilyRequest):
    """
    创建家庭
    :param request: 请求对象
    :param data: 请求数据
    :return: 操作结果
    """
    user_id = request.state.user_id
    return await family_service.create_family(user_id, data.family_name)

# 获取家庭成员列表
@family_router.get("/get_family_members", summary="获取家庭成员列表")
@token_required
@log_required
async def get_family_members(request: Request):
    """
    获取家庭成员列表
    :param request: 请求对象
    :return: 家庭成员列表
    """
    user_id = request.state.user_id
    return await family_service.get_family_members(user_id)

# 获取家庭账单列表
@family_router.get("/get_family_bills", summary="获取家庭账单列表")
@token_required
@log_required
async def get_family_bills(
    request: Request,
    bill_type: Optional[str] = Query(None, description="账单类型"),
    type: Optional[str] = Query(None, description="支付类型"),
    payment_status: Optional[str] = Query(None, description="支付状态"),
    payment_method: Optional[str] = Query(None, description="支付方式"),
    member: Optional[str] = Query(None, description="成员ID"),
    current_page: int = Query(1, description="当前页码"),
    page_size: int = Query(10, description="每页数量")
):
    """
    获取家庭账单列表
    :param request: 请求对象
    :param bill_type: 账单类型
    :param type: 支付类型
    :param payment_status: 支付状态
    :param payment_method: 支付方式
    :param member: 成员ID
    """
    user_id = request.state.user_id
    
    # 转换参数类型
    type_int = int(type) if type and type.isdigit() else None
    payment_status_int = int(payment_status) if payment_status and payment_status.isdigit() else None
    member_int = int(member) if member and member.isdigit() else None
    
    return await family_service.get_family_bills(
        user_id,
        bill_type,
        type_int,
        payment_status_int,
        payment_method,
        member_int,
        current_page,
        page_size
    )

# 获取家庭信息
@family_router.get("/get_family_info", summary="获取家庭信息")
@token_required
@log_required
async def get_family_info(request: Request):
    user_id = request.state.user_id
    return await family_service.get_family_info(user_id)

# 获取家庭预算列表
@family_router.get("/get_family_budgets", summary="获取家庭预算列表")
@token_required
@log_required
async def get_family_budgets(request: Request):
    """
    获取家庭预算列表
    :param request: 请求对象
    :return: 家庭预算列表
    """
    user_id = request.state.user_id
    return await family_service.get_family_budgets(user_id)

class RemoveMemberRequest(BaseModel):
    user_id: int

@family_router.post("/remove_family_member", summary="移出家庭成员")
@token_required
@log_required
async def remove_member(
    request: Request,
    data: RemoveMemberRequest
):
    """
    移出家庭成员
    :param request: 请求对象
    :param data: 请求数据
    :return: 操作结果
    """
    user_id = request.state.user_id
    return await family_service.remove_family_member(user_id, data.user_id)

class SetAdminRequest(BaseModel):
    user_id: int
    is_promote: bool

@family_router.post("/set_family_admin", summary="设置家庭管理员")
@token_required
@log_required
async def set_admin(
    request: Request,
    data: SetAdminRequest
):
    """
    设置家庭管理员
    :param request: 请求对象
    :param data: 请求数据
    :return: 操作结果
    """
    user_id = request.state.user_id
    return await family_service.set_family_admin(user_id, data.user_id, data.is_promote)

@family_router.get("/check_family", summary="查询用户家庭状态")
@token_required
@log_required
async def check_family(request: Request):
    """
    查询用户家庭状态
    :param request: 请求对象
    :return: 用户家庭信息
    """
    user_id = request.state.user_id
    return await family_service.check_family(user_id)

@family_router.post("/delete_family", summary="删除家庭")
@token_required
@log_required
async def delete_family(request: Request):
    """
    删除家庭
    :param request: 请求对象
    :return: 操作结果
    """
    user_id = request.state.user_id
    return await family_service.delete_family(user_id)

class ValidatePhoneRequest(BaseModel):
    phone: str

class SendInviteRequest(BaseModel):
    phone: str

@family_router.post("/validate_phone", summary="验证手机号")
@token_required
@log_required
async def validate_phone(request: Request, data: ValidatePhoneRequest):
    """
    验证手机号
    :param request: 请求对象
    :param data: 请求数据
    :return: 验证结果
    """
    user_id = request.state.user_id
    return await family_service.validate_phone(user_id, data.phone)

@family_router.post("/send_invite", summary="发送家庭邀请")
@token_required
@log_required
async def send_invite(request: Request, data: SendInviteRequest):
    """
    发送家庭邀请
    :param request: 请求对象
    :param data: 请求数据
    :return: 操作结果
    """
    user_id = request.state.user_id
    return await family_service.send_invite(user_id, data.phone)


class JoinFamilyRequest(BaseModel):
    invite_code: str


@family_router.post("/join_family", summary="加入家庭")
@token_required
@log_required
async def send_invite(request: Request, data: JoinFamilyRequest):
    """
    加入家庭
    :param request: 请求对象
    :param data: 请求数据
    :return: 操作结果
    """
    user_id = request.state.user_id
    return await family_service.join_family(user_id, data.invite_code)
